package com.revenant.driver.receiver;

import android.app.Notification;
import android.app.usage.UsageEvents;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;

import com.apkfuns.logutils.LogUtils;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.revenant.driver.App;
import com.revenant.driver.bean.BaseBean.MessageEvent;
import com.revenant.driver.bean.NotifictionType;
import com.revenant.driver.ui.XieYiActivity;
import com.revenant.driver.ui.activity.DriverGoodsDetailActivity;
import com.revenant.driver.ui.activity.DriverMainActivity;
import com.revenant.driver.ui.activity.DriverMineActivity;
import com.revenant.driver.ui.activity.DriverOrderActivity;
import com.revenant.driver.ui.activity.MessageActivity;
import com.revenant.driver.ui.activity.NewsActivity;
import com.revenant.driver.utils.EventBusUtil;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static com.revenant.driver.bean.BaseBean.MyEventCode.LianWangPush;
import static com.revenant.driver.bean.BaseBean.MyEventCode.PingTaiPush;
import static com.revenant.driver.bean.BaseBean.MyEventCode.PingTai_Push_Code;
import static com.revenant.driver.bean.BaseBean.MyEventCode.RenZhengCode;
import static com.revenant.driver.bean.BaseBean.MyEventCode.RenZhengCode32;
import static com.revenant.driver.bean.BaseBean.MyEventCode.Xiao_Personal_Push_Code_NeWs;
import static com.revenant.driver.bean.BaseBean.MyEventCode.Xiao_Sys_Push_Code_NeWs;
import static com.revenant.driver.bean.BaseBean.MyEventCode.ZhiPai_Push_Code_NeWs;

public class MyJpushReceiver extends JPushMessageReceiver {
    private static final String TAG = "PushMessageReceiver";
    public MyJpushReceiver() {
        super();
    }

    @Override
    public Notification getNotification(Context context, NotificationMessage notificationMessage) {
        return super.getNotification(context, notificationMessage);
    }

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        Log.e(TAG,"[onMessage] "+customMessage);
//        MessageEvent event = new MessageEvent(PingTai_Push_Code);
//        EventBusUtil.sendStickyEvent(event);
        String custom = customMessage.extra;
        if (!custom.isEmpty()) {
            NotifictionType customType = GsonUtil.parseJsonWithGson(custom, NotifictionType.class);
            switch (Integer.valueOf(customType.getMsgId())) {
                case 10020:
//                    MessageEvent event = new MessageEvent(PingTai_Push_Code);
//                    EventBusUtil.sendStickyEvent(event);
                    break;
                case 30012:/*司机实名认证成功*/
                    MessageEvent event3012 = new MessageEvent(RenZhengCode);
                    EventBusUtil.sendStickyEvent(event3012);
                    break;
                case 30031:/*司机实名认证成功*/
                    MessageEvent event3031 = new MessageEvent(RenZhengCode);
                    EventBusUtil.sendStickyEvent(event3031);
                    break;
                case 30032:/*司机实名认证失败*/
                    MessageEvent event3032 = new MessageEvent(RenZhengCode);
                    MessageEvent event3032b = new MessageEvent(RenZhengCode32);
                    EventBusUtil.sendStickyEvent(event3032);
                    EventBusUtil.sendStickyEvent(event3032b);
                    break;
                case 50020:
                    MessageEvent event20 = new MessageEvent(PingTai_Push_Code);
                    EventBusUtil.sendStickyEvent(event20);
                    music(context);
                    break;
                case 50030:
                    MessageEvent event30 = new MessageEvent(PingTai_Push_Code);
                    EventBusUtil.sendStickyEvent(event30);
                    music(context);
                    break;
                case 50040:
                    music(context);
                    break;
            }
        }


    }


    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
        Log.e(TAG,"[onMessage] "+notificationMessage);
        String notificationExtras = notificationMessage.notificationExtras;

//        AppUtils.relaunchApp();

        if (!notificationExtras.isEmpty()) {
            NotifictionType notifictionType = GsonUtil.parseJsonWithGson(notificationExtras, NotifictionType.class);
//            SPUtils.setPushCode(context, ZhiPai_Push_Code_NeWs);

//            jumpActivity(context,NewsActivity.class);

            switch (Integer.valueOf(notifictionType.getMsgId())) {

                case 10010: /*系统消息*/
//                    jumpActivity(context, NewsActivity.class);
//                    SPUtils.setPushCode(context, Xiao_Sys_Push_Code_NeWs);
                    Bundle bundlesys = new Bundle();
                    bundlesys.putInt("newsselect", 0);
                    BundleActivity(context, NewsActivity.class, bundlesys);
                    break;
                case 10020:/*私人消息*/
//                    SPUtils.setPushCode(context, Xiao_Personal_Push_Code_NeWs);
//                    Intent intent = new Intent();
//                    intent.setClass(context, NewsActivity.class);
//                    context.startActivity(intent);
//                    jumpActivity(context, NewsActivity.class);
                    Bundle bundleper = new Bundle();
                    bundleper.putInt("newsselect", 1);
                    BundleActivity(context, NewsActivity.class, bundleper);
                    break;
                /*磅单类型*/
                case 20010:/*司机上传装货磅单*/
//                    jumpActivity(context, DriverOrderActivity.class);
                    break;
                case 20020:/*司机上传卸货磅单*/
//                    jumpActivity(context, DriverOrderActivity.class);
                    break;
                case 20030:/*货主上传磅单*/
//                    jumpActivity(context, NewsActivity.class);
//                    Bundle bundle20030 = new Bundle();
//                    bundle20030.putInt("bundle", 1);
//                    BundleActivity(context, DriverOrderActivity.class, bundle20030);
                    jumpActivity(context, DriverOrderActivity.class);
                    break;
                /*认证类型*/
                case 30010:/*货主个人认证*/
                    break;
                case 30011:/*货主个人认证成功*/
                    break;
                case 30012:/*货主个人认证失败*/
                    break;
                case 30020:/*货主企业认证*/
                    break;
                case 30021:/*货主企业认证成功*/
                    break;
                case 30022:/*货主企业认证失败*/
                    break;
                case 30030:/*司机认证*/
                    jumpActivity(context, DriverMineActivity.class);
                    break;
                case 30031:/*司机实名认证成功*/
                    jumpActivity(context, DriverMineActivity.class);
                    break;
                case 30032:/*司机实名认证失败*/
                    jumpActivity(context, DriverMineActivity.class);
                    break;
                /*结算类型*/
                case 40010:/*结算成功*/
                    break;
                case 40020:/*结算失败*/
                    break;
                /*货源推送类型*/
                case 50010:/*平台货源*/
//                    MessageEvent event50010 = new MessageEvent(PingTaiPush);
//                    EventBusUtil.sendStickyEvent(event50010);
                    Integer integerpt = Integer.valueOf(notifictionType.getGoodsId());
                    if (integerpt > 0) {
                        Bundle bundlept = new Bundle();
                        bundlept.putInt("goods_type", 0);
                        bundlept.putString("goods_id", String.valueOf(integerpt));
                        BundleActivity(context, DriverGoodsDetailActivity.class, bundlept);
                    }


                    break;
                case 50020:/*熟车群抢单货源*/
                    SPUtils.setPushCode(context, ZhiPai_Push_Code_NeWs);
//                    MessageEvent event20 = new MessageEvent(PingTai_Push_Code_NeWs);
//                    EventBusUtil.sendEvent(event20);

//                    LogUtils.d("推送" + Integer.valueOf(notifictionType.getMsgId()));
//                    jumpActivity(context, NewsActivity.class);
                    break;
                case 50021:/*联网货源*/
//                    MessageEvent event50021= new MessageEvent(LianWangPush);
//                    EventBusUtil.sendStickyEvent(event50021);
                    Integer integerlw = Integer.valueOf(notifictionType.getGoodsId());
                    if (integerlw > 0) {
                        Bundle bundlept = new Bundle();
                        bundlept.putInt("goods_type", 2);
                        bundlept.putString("goods_id", String.valueOf(integerlw));
                        BundleActivity(context, DriverGoodsDetailActivity.class, bundlept);
                    }
                    break;
                case 50030:/*熟车指派货源*/
                 Integer integerptzp = Integer.valueOf(notifictionType.getGoodsId());
                    if (integerptzp > 0) {
                        Bundle bundlept = new Bundle();
                        bundlept.putInt("goods_type", 1);
                        bundlept.putString("goods_id", String.valueOf(integerptzp));
                        BundleActivity(context, DriverGoodsDetailActivity.class, bundlept);
                    }

                    break;
                case 50040:/*为空车推送货源*/
                    break;
                /*发票类型*/
                case 60011:/*发票开出成功*/
                    break;
                case 60012:/*发票开出失败*/
                    break;
                case 70000:
                    Bundle bundlxieyi = new Bundle();
                    bundlxieyi.putString("weburl", notifictionType.getUrl());
                    bundlxieyi.putString("conorderId", notifictionType.getGoodsId());
                    BundleActivity(context, XieYiActivity.class, bundlxieyi);
                    break;
                /*金融类型*/
                case 70010:/*充值*/
                    break;
                case 70011:/*充值成功*/
                    break;
                case 70012:/*充值失败*/
                    break;
                case 70020:/*提现*/
                    break;
                case 70021:/*提现成功*/
                    break;
                case 70022:/*提现失败*/
                    break;
                case 70030:/*转账成功*/
                    break;
                case 70031:/*转账失败*/
                    break;
                case 70040:/*绑定银行卡*/
                    break;
                case 70041:/*绑定成功*/
                    break;
                case 70050:/*更改支付密码*/
                    break;
                case 70051:/*更改成功*/
                    break;
                case 70052:/*更改失败*/
                    break;

//                    /*其他*/
                case 80011:/*取消订单成功*/
                    break;
                case 80012:/*取消订单失败*/
                    break;
                case 90012:
                    Bundle bundlezidong = new Bundle();
                    bundlezidong.putInt("orderselect", 1);
                    BundleActivity(context, DriverOrderActivity.class, bundlezidong);

                    break;
            }
        }


    }

    //接收到推送消息
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
        String title = notificationMessage.notificationTitle;
        String notificationContent = notificationMessage.notificationContent;
        Log.e(TAG,"[onNotifyMessageArrived] "+notificationMessage);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
        Log.e(TAG,"[onNotifyMessageDismiss] "+notificationMessage);

    }

    @Override
    public void onRegister(Context context, String registrationId) {
        super.onRegister(context, registrationId);
        Log.e(TAG,"[onRegister] "+registrationId);
    }

    //连接极光
    @Override
    public void onConnected(Context context, boolean isConnected) {
        super.onConnected(context, isConnected);
        Log.e(TAG,"[onConnected] "+isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        super.onCommandResult(context, cmdMessage);
        Log.e(TAG,"[onCommandResult] "+cmdMessage);
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        super.onMultiActionClicked(context, intent);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    private void jumpActivity(Context context, Class<?> clz) {
        try {
            Intent intent = new Intent();
            intent.setClass(context, clz);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);

        } catch (Throwable throwable) {

        }

    }

    public void jumpBundleActivity(Context context, Class<?> clz, Bundle bundle) {
        try {
            Intent intent = new Intent();
            intent.setClass(context, clz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


            context.startActivity(intent);

        } catch (Throwable throwable) {

        }

    }

    public void BundleActivity(Context context, Class<?> clz, Bundle bundle) {
        try {
            Intent intent = new Intent();
            intent.setClass(context, clz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);

        } catch (Throwable throwable) {

        }

    }

    private void music(Context context) {
        AssetManager assetManager;
        MediaPlayer player = null;
        player = new MediaPlayer();
        assetManager = context.getResources().getAssets();
        try {
            AssetFileDescriptor fileDescriptor = assetManager.openFd("dingyuep2.wav");
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getStartOffset());
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}